package com.zfzs.post.service.impl;

import com.zfzs.post.exception.BadRequestException;
import com.zfzs.post.model.dto.MineBenefitDetailDTO;
import com.zfzs.post.model.vo.benefit.BenefitDay4MineVO;
import com.zfzs.post.model.vo.benefit.MineDetailHeadVO;
import com.zfzs.post.modules.agent.service.dto.AgentDTO;
import com.zfzs.post.modules.transaction.domain.BenefitDay;
import com.zfzs.post.modules.transaction.service.BenefitDayService;
import com.zfzs.post.modules.transaction.service.dto.BenefitDay4MineDTO;
import com.zfzs.post.service.BenefitService;
import com.zfzs.post.service.JwtUserDetailsService;
import com.zfzs.post.utils.CloneUtils;
import com.zfzs.post.utils.DateStrUtils;
import com.zfzs.post.utils.MathUtils;
import com.zfzs.post.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wq
 * @date 2019/10/21 18:10
 * @description
 */
@Service
public class BenefitServiceImpl implements BenefitService {

    @Autowired
    private BenefitDayService benefitDayService;

    @Autowired
    private  JwtUserDetailsService jwtUserDetailsService;

    @Override
    public List<BenefitDay> findListByAgentId(Long agentId) {
        return findListByAgentId(agentId,null,null);
    }

    @Override
    public BigDecimal getSelfBenefit(Long agentId) {
        return getSelfBenefit(agentId,null,null);
    }

    @Override
    public BigDecimal getTeamBenefit(Long agentId) {
        return getTeamBenefit(agentId,null,null);
    }

    @Override
    public List<BenefitDay> findListByAgentId(Long agentId, String startDate, String endDate) {
        if(StringUtils.isEmpty(startDate) && StringUtils.isEmpty(endDate)){
            return benefitDayService.findByAgentId(agentId);
        }else {
            return benefitDayService.findByAgentId(agentId,startDate,endDate);
        }
    }

    @Override
    public BigDecimal getSelfBenefit(Long agentId,String startDate,String endDate) {
            List<BenefitDay> byAgentId = findListByAgentId(agentId,startDate,endDate);
        return byAgentId.stream().map(BenefitDay::getSelfBenefit).reduce(BigDecimal.ZERO,BigDecimal::add);
    }

    @Override
    public BigDecimal getTeamBenefit(Long agentId,String startDate,String endDate) {
        List<BenefitDay> byAgentId = findListByAgentId(agentId);
        return byAgentId.stream().map(BenefitDay::getTeamBenefit).reduce(BigDecimal.ZERO,BigDecimal::add);
    }

    @Override
    public MineDetailHeadVO getMineDetailHead(Long agentId, String startDate, String endDate, Integer isSelf) {

        String pastMonth = DateStrUtils.getMonthOffsetStr(DateStrUtils.Y_M, -1);

        BigDecimal pastBenefit =isSelf.intValue()==1? benefitDayService.findSelfBenefitByAgentId(agentId, pastMonth):benefitDayService.findTeamBenefitByAgentId(agentId,pastMonth);

        String pastTwoMonth = DateStrUtils.getMonthOffsetStr(DateStrUtils.Y_M, -2);

        BigDecimal pastTwoBenefit = isSelf.intValue()==1?benefitDayService.findSelfBenefitByAgentId(agentId, pastTwoMonth):benefitDayService.findTeamBenefitByAgentId(agentId,pastMonth);
        String rateStr;
        if(pastTwoBenefit.compareTo(new BigDecimal("0"))==0){
            rateStr="-";
        }else {
            BigDecimal  rate=pastBenefit.subtract(pastTwoBenefit).multiply(new BigDecimal("100")).divide(pastTwoBenefit,2,BigDecimal.ROUND_HALF_UP);
            rateStr=rate+"%";
        }


        BigDecimal curMonthBenefit=pastBenefit.multiply(pastBenefit).add(pastBenefit);

        BigDecimal total=isSelf.intValue()==1?getSelfBenefit(agentId,startDate,endDate):getTeamBenefit(agentId,startDate,endDate);

        MineDetailHeadVO vo=new MineDetailHeadVO();

        vo.setCurrMonth(MathUtils.formatToNumber(curMonthBenefit));
        vo.setPastMonth(MathUtils.formatToNumber(pastBenefit));
        vo.setPastTwoMonth(MathUtils.formatToNumber(pastTwoBenefit));
        vo.setTotalBenefit(MathUtils.formatToNumber(total));
        vo.setRate(rateStr);
        return vo;
    }


    @Override
    public List<BenefitDay4MineVO> getMineBenefitList(Long agentId, String startDate, String endDate, Integer isSelf, Integer pageNum) {
        List<BenefitDay4MineDTO> mineDetail = benefitDayService.findMineDetail(agentId, startDate, endDate, isSelf,pageNum);
        return CloneUtils.batchClone(mineDetail,BenefitDay4MineVO.class);
    }

    @Override
    public Map<String, Object> getMineBenefitDetail(MineBenefitDetailDTO dto) {
        boolean startFlag= StringUtils.isEmpty(dto.getStartDate());
        boolean endFlag=StringUtils.isEmpty(dto.getEndDate());

        if(startFlag!=endFlag){
            throw new BadRequestException("起止时间必须同时选择");
        }
        AgentDTO agentInfo = jwtUserDetailsService.getAgentInfo();

        MineDetailHeadVO mineDetailHead = getMineDetailHead(agentInfo.getId(), dto.getStartDate(), dto.getEndDate(), dto.getIsSelf());
        List<BenefitDay4MineVO> mineBenefitList = getMineBenefitList(agentInfo.getId(), dto.getStartDate(), dto.getEndDate(), dto.getIsSelf(),dto.getPageNum());
        Map<String,Object> result=new HashMap<>(2);
        result.put("mineDetailHead",mineDetailHead);
        result.put("mineBenefitList",mineBenefitList);
        return result;
    }


}
